/*
 * Filename TestOrderItemRepoositoryImpl.java 2022年03月18日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.core.api.dto.order.TestOrderItemDTO;
import com.ehome.poct.core.api.dto.report.ReportItemDTO;
import com.ehome.poct.core.dao.entity.order.TestOrderItemDO;
import com.ehome.poct.core.dao.mybatis.mapper.order.TestOrderItemMapper;
import com.ehome.poct.core.repository.order.TestOrderItemRepository;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wyc
 * @since 2022/03/18 14:05
 **/
@Repository
public class TestOrderItemRepositoryImpl extends AbstractCrudRepository<TestOrderItemDO, TestOrderItemDTO> implements TestOrderItemRepository {
    @Autowired
    private TestOrderItemMapper testOrderItemMapper;
    /**
     * 双联试纸
     */
    private static final String S_CRP_SAA_REPORT = "S_CRP_SAA_REPORT";

    private static final String U_CRP_SAA_REPORT = "U_CRP_SAA_REPORT";

    @Override
    public void updateByOrderNo(ReportItemDTO r, String orderNo) {
        //不区分双联试纸
        if (S_CRP_SAA_REPORT.equals(r.getReportCode())) {
            r.setReportCode(U_CRP_SAA_REPORT);
        }
        testOrderItemMapper.update(null, Wrappers.<TestOrderItemDO>lambdaUpdate().set(TestOrderItemDO::getTestPoctReportId, r.getReportId()).eq(TestOrderItemDO::getTestOrderNo, orderNo).eq(TestOrderItemDO::getTestReportTypeCode, r.getReportCode()));
    }

    @Override
    public void updateByOrderNO(Long bloodCheckReportId, String orderNo, Long stripTypeCode) {
        testOrderItemMapper.update(null, Wrappers.<TestOrderItemDO>lambdaUpdate()
                .set(TestOrderItemDO::getTestPoctReportId, bloodCheckReportId)
                .eq(TestOrderItemDO::getTestOrderNo, orderNo)
                .eq(TestOrderItemDO::getStripTypeCode, stripTypeCode));
    }

    @Override
    public void delOrderItem(ReportItemDTO reportItemDTO, String orderNo) {
        QueryWrapper<TestOrderItemDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TestOrderItemDO::getTestOrderNo, orderNo).eq(TestOrderItemDO::getStripTypeCode, reportItemDTO.getStripeCode());
        testOrderItemMapper.delete(queryWrapper);
    }

    @Override
    public void updateItemStripTypeCode(String orderNo, Integer stripTypeCode) {
        UpdateWrapper<TestOrderItemDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(TestOrderItemDO::getStripTypeCode, stripTypeCode)
                .set(TestOrderItemDO::getTestReportTypeCode, "S_CRP_SAA_REPORT")
                .set(TestOrderItemDO::getOrderItemName, "CRP+SAA双联试纸检测")
                .eq(TestOrderItemDO::getTestOrderNo, orderNo)
                .eq(TestOrderItemDO::getTestReportTypeCode, "U_CRP_SAA_REPORT");
        testOrderItemMapper.update(null, updateWrapper);
    }

    @Override
    public void updateStripTypeCodeByTestOrderNoAndStripTypeCode(Integer updatedStripTypeCode, String testOrderNo, Integer stripTypeCode) {
        testOrderItemMapper.updateStripTypeCodeByTestOrderNoAndStripTypeCode(updatedStripTypeCode, testOrderNo, stripTypeCode);
    }

    @Override
    public void updateByTestOrderNoAndStripTypeCode(TestOrderItemDTO updateItemDTO, String testOrderNo, Integer stripTypeCode) {
        TestOrderItemDO testOrderItemDO = BeanUtil.copyProperties(updateItemDTO, TestOrderItemDO.class);
        testOrderItemMapper.updateByTestOrderNoAndStripTypeCode(testOrderItemDO, testOrderNo, stripTypeCode);
    }

    @Override
    public List<TestOrderItemDTO> findList(String testOrderNo, List<Integer> stripTypeCodeList) {
        List<TestOrderItemDO> orderItemDOList = testOrderItemMapper.findByTestOrderNoAndStripTypeCodeIn(testOrderNo, stripTypeCodeList);
        if (CollUtil.isEmpty(orderItemDOList)) {
            return Lists.newArrayList();
        }
        return BeanUtil.copyToList(orderItemDOList, TestOrderItemDTO.class);
    }
}